package com.nlx.notes.module.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nlx.notes.module.bean.dto.FlowSaveDTO;
import com.nlx.notes.module.bean.dto.flow.UpdateDBAmountDTO;
import com.nlx.notes.module.bean.dto.querydb.FlowMonthQueryDbDTO;
import com.nlx.notes.module.core.enums.TransactionTypeEnum;
import com.nlx.notes.module.core.util.AmountConverterUtils;
import com.nlx.notes.module.core.util.DateUtils;
import com.nlx.notes.module.entity.FlowMonthStatistics;
import com.nlx.notes.module.mapper.FlowMonthStatisticsMapper;
import com.nlx.notes.module.service.IFlowMonthStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nlx
 * @since 2024-12-15
 */
@Service
public class FlowMonthStatisticsServiceImpl extends ServiceImpl<FlowMonthStatisticsMapper, FlowMonthStatistics> implements IFlowMonthStatisticsService {

    @Override
    public FlowMonthStatistics findByMonth(String month,Long bookId,Long userId) {
        if(StrUtil.isEmpty(month) || null == userId){
            return null;
        }
        return getBaseMapper().selectOne(Wrappers.lambdaQuery(FlowMonthStatistics.class)
                .eq(FlowMonthStatistics::getUserId,userId)
                .eq(FlowMonthStatistics::getBookId,bookId)
                .eq(FlowMonthStatistics::getMonthTime,month)
                .eq(FlowMonthStatistics::getDelFlag,0),false);
    }

    @Override
    public void saveMonthFlow(FlowSaveDTO flowSaveDTO) {
        String month = DateUtils.formatMonth(flowSaveDTO.getTransactionDate());
        int monthAmount = AmountConverterUtils.convertYuanToFen(flowSaveDTO.getAmount());
        FlowMonthStatistics statistics = this.findOneByMonth(month,flowSaveDTO.getUserId());
        if(null == statistics){
            FlowMonthStatistics flowMonthStatistics = new  FlowMonthStatistics();
            if(TransactionTypeEnum.INCOME.getCode().equalsIgnoreCase(flowSaveDTO.getTransactionType())){
                flowMonthStatistics.setIncome(monthAmount);
            }else{
                flowMonthStatistics.setExpense(monthAmount);
            }
            flowMonthStatistics.setMonthTime(month);
            flowMonthStatistics.setTransactionType(flowSaveDTO.getTransactionType());
            flowMonthStatistics.setUserId(flowSaveDTO.getUserId());
            flowMonthStatistics.setDelFlag(0);
            this.save(flowMonthStatistics);
        }else{
            this.incMonthAmount(statistics.getId(),monthAmount,flowSaveDTO.getTransactionType());
        }
    }

    @Override
    public void deleteFlow(UpdateDBAmountDTO updateDBAmountDTO) {
        String month = DateUtils.formatMonth(updateDBAmountDTO.getDate());
        FlowMonthStatistics flowMonthStatistics = this.findByDayAndType(month,updateDBAmountDTO.getUserId());
        if(null == flowMonthStatistics){
            return ;
        }
        updateDBAmountDTO.setId(flowMonthStatistics.getId());
        incMonthAmount(flowMonthStatistics.getId(),updateDBAmountDTO.getAmount(), updateDBAmountDTO.getTransactionType());
        //通过天查询数据
//        this.getBaseMapper().decMonthAmount(flowMonthStatistics.getId(),updateDBAmountDTO.getAmount());
    }

    @Override
    public List<FlowMonthStatistics> findByCondition(FlowMonthQueryDbDTO flowMonthQueryDbDTO) {
        return this.getBaseMapper().findByCondition(flowMonthQueryDbDTO);
    }

    @Override
    public FlowMonthStatistics findMaxAmountByYear(String year, String type,Long bookId, Long userId) {
        if(TransactionTypeEnum.EXPEND.getCode().equalsIgnoreCase(type)){
            return this.getBaseMapper().findExpenseMaxAmountByYear(year,bookId,userId);
        }else{
            return this.getBaseMapper().findIncomeMaxAmountByYear(year,bookId,userId);
        }
    }

    private FlowMonthStatistics findByDayAndType(String date, Long userId) {
        if(StrUtil.isEmpty(date)  || null == userId ){
            return null;
        }
        return this.getBaseMapper().selectOne(Wrappers.lambdaQuery(FlowMonthStatistics.class)
                .eq(FlowMonthStatistics::getUserId,userId)
                .eq(FlowMonthStatistics::getMonthTime,date)
                .eq(FlowMonthStatistics::getDelFlag,0).last(" limit 1"));
    }

    private void incMonthAmount(Long id, int monthAmount,String transactionType) {
        if(id == null || 0 == monthAmount){
            return ;
        }
        if(TransactionTypeEnum.INCOME.getCode().equalsIgnoreCase(transactionType)){
            this.getBaseMapper().incMonthIncomeAmount(id,monthAmount);
        }else{
            this.getBaseMapper().incMonthExpenseAmount(id,monthAmount);
        }
    }

    private FlowMonthStatistics findOneByMonth(String month, Long userId) {
        if(StrUtil.isEmpty(month) || userId == null){
            return null;
        }
        return this.getBaseMapper().selectOne(Wrappers.lambdaQuery(FlowMonthStatistics.class)
                .eq(FlowMonthStatistics::getUserId,userId)
                .eq(FlowMonthStatistics::getMonthTime,month)
                .eq(FlowMonthStatistics::getDelFlag,0)
                .last(" limit 1"));
    }
}
